package com.blkj.iam.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blkj.iam.common.model.Option;
import com.blkj.iam.core.security.util.SecurityUtils;
import com.blkj.iam.system.converter.TenantConverter;
import com.blkj.iam.system.mapper.ClientMapper;
import com.blkj.iam.system.mapper.TenantMapper;
import com.blkj.iam.system.model.entity.Client;
import com.blkj.iam.system.model.entity.Tenant;
import com.blkj.iam.system.model.form.TenantForm;
import com.blkj.iam.system.model.query.TenantPageQuery;
import com.blkj.iam.system.model.vo.TenantPageVO;
import com.blkj.iam.system.service.TenantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: 系统租户表
 * @Author: jeecg-boot
 * @Date: 2025-04-10
 * @Version: V1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements TenantService {

    private final ClientMapper clientMapper;
    private final TenantMapper tenantMapper;
    private final TenantConverter tenantConverter;


    @Override
    public IPage<TenantPageVO> getTenantPage(TenantPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Tenant> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<Tenant> tenantPage = this.page(page, new LambdaQueryWrapper<Tenant>().like(ObjectUtil.isNotEmpty(queryParams.getName()), Tenant::getName, queryParams.getName()));

        // 实体转换
        return tenantConverter.toPageVo(tenantPage);
    }

    @Override
    public List<Option<Long>> listClientOptions() {
        List<Client> list = clientMapper.selectList(new LambdaQueryWrapper<Client>().orderByAsc(Client::getCreateTime));
        return CollectionUtil.emptyIfNull(list).stream().map(client -> new Option<Long>(client.getId(), client.getAppName())).collect(Collectors.toList());
    }

    @Override
    public TenantForm getTenantFormData(Long tenantId) {
        Tenant tenant = this.getById(tenantId);
        return tenantConverter.toForm(tenant);
    }

    @Override
    public boolean saveTenant(TenantForm tenantForm) {
        // 实体转换 form->entity
        Tenant entity = tenantConverter.toEntity(tenantForm);
        entity.setCreateBy(SecurityUtils.getUserId());

        Client client = clientMapper.selectById(entity.getClientId());
        entity.setAppName(client.getAppName());
        return this.save(entity);
    }

    @Override
    public boolean updateTenant(Long tenantId, TenantForm tenantForm) {
        // 实体转换 form->entity
        Tenant entity = tenantConverter.toEntity(tenantForm);
        entity.setUpdateBy(SecurityUtils.getUserId());

        Client client = clientMapper.selectById(entity.getClientId());
        entity.setAppName(client.getAppName());
        return this.updateById(entity);
    }

    @Override
    public boolean deleteTenants(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的租户数据为空");
        // 将字符串 ID 列表转换为 Long 类型的列表
        List<Long> tenantIds = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return tenantMapper.deleteByIds(tenantIds) > 0;
    }
}
